$(function () {
    xsdexamine_init();
})


function xsdexamine_init() {

    $("[name='xsdexamine_sbgs']").jgbmtree($("[name='xsdexamine_sbbm']"), $("[name='xsdexamine_sbr']"), '', '')
    //客户加载
    $("[name='xsdexamine_kh']").qyjgzcombobox();
    //工程加载
    $("[name='xsdexamine_gc']").cxgccombobox();
    var options = getXsdexamineGridOption();
    $('#xsdexamine-grid').datagrid(options);
}

function xsdexamine_search() {
    var zt = $("#xsdexamine-toolbar input[name='xsdexamine_zt']").val();
    var xsdh = $("#xsdexamine-toolbar input[name='xsdexamine_xsdh']").val();
    var kh = $("#xsdexamine-toolbar input[name='xsdexamine_kh']").val();
    //var sbdw = $("#xsd-toolbar input[name='xsdSbdw']").val();
    var sbgs = $("#xsdexamine-toolbar input[name='xsdexamine_sbgs']").val();
    var sbbm = $("#xsdexamine-toolbar input[name='xsdexamine_sbbm']").val();
    var sbr = $("#xsdexamine-toolbar input[name='xsdexamine_sbr']").val();
    var gc = $("#xsdexamine-toolbar input[name='xsdexamine_gc']").val();
    var fhsjStart = $('#fhrqexamine_start').datebox('getValue');
    var fhsjEnd = $('#fhrqexamine_end').datebox('getValue');
    var options = $.extend(getXsdexamineGridOption(),
        {queryParams: {zt: zt, xsdh: xsdh, khId: kh, sbgsId: sbgs, sbbmId: sbbm, sbrId: sbr, gcId: gc, fhsjStart: fhsjStart, fhsjEnd: fhsjEnd}});
    $('#xsdexamine-grid').datagrid(options);
}


//新增、修改销售单弹出界面
var xsdexamine_win;

function xsd_examineItem() {
    var record = $("#xsdexamine-grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要审核的记录!", "info");
        return;
    }
    var href = $.webPath() + "/xsgl/xsdgl/xsdgl_examine?xsdId=" + record.id;
    var button = [
        {text: '审核', handler: examineXsd}
    ];

    xsdexamine_win = $.window({
        title: "审核销售单",
        iconCls: 'icon-add2',
        url: href,
        width: 1024,
        height: 505,
        buttons: button
    });
}

function examineXsd() {
    var record = $("#xsdexamine-grid").datagrid("getSelected");
    $.post($.webPath() + '/xsgl/xsdgl/examine_xsd', {xsdId: record.id}, function (data) {
        var obj = eval('(' + data + ')');
        if (obj.statusCode == '200') {
            $(xsdexamine_win).window('close');
            $('#xsdexamine-grid').datagrid('reload');
            $('#xsdexamine_layout').layout('panel', 'center').panel('refresh');
            $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
        } else
            $.messager.alert('提示', obj.message, 'info');
    });
}

function xsd_cancelexamineItem(){
    var record = $("#xsdexamine-grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要取消审核的记录!", "info");
        return;
    }
    $.post($.webPath() + '/xsgl/xsdgl/cancelexamine_xsd', {xsdId: record.id}, function (data) {
        var obj = eval('(' + data + ')');
        if (obj.statusCode == '200') {
            $('#xsdexamine-grid').datagrid('reload');
            $('#xsdexamine_layout').layout('panel', 'center').panel('refresh');
            $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
        } else
            $.messager.alert('提示', obj.message, 'info');
    });
}

function getXsdexamineGridOption() {
    var options = {
        url: $.webPath() + "/xsgl/xsdgl/xsdexamineList?sftzd=false",
        toolbar: "#xsdexamine-toolbar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: true,
        showFooter: true,
        onLoadSuccess: function (target) {
            $(this).datagrid('selectRow', 0);

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.xslx) {
                    this.xslx = this.xslx == 'htxs' ? "合同销售" : this.xslx == 'zddh' ? "直达到货" : this.xslx == 'bgxs' ? "保供销售" : "库存零售";
                }
            })
            return data;
        },
        onSelect: function (rowIndex, rowData) {
            if (rowData != null && rowData.zt == 'ytj' && !rowData.cwsh) {
                $("#xsd-examine").show();
                $("#xsd-cancelexamine").hide();
            }else if(rowData != null && rowData.zt != 'ysp' && rowData.cwsh){
                $("#xsd-examine").hide();
                $("#xsd-cancelexamine").hide();
            } else {
                $("#xsd-examine").hide();
                $("#xsd-cancelexamine").hide();
            }
        }
    }

    var columns = {columns: [
        [
            {field: 'xsdh', title: '销售单号', align: 'center', width: 50, formatter: xpdCell},
            {field: 'xslx', title: '销售类型', align: 'center', width: 30},
            {field: 'fhsj', title: '发货日期', align: 'center', width: 35},
            {field: 'sxsj', title: '制单日期', align: 'center', width: 35},
            {field: 'kh_jc', title: '客户名称', align: 'center', width: 50},
            {field: 'xssl', title: '销售数量', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }
            },
            {field: 'xsje', title: '销售金额(元)', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }
            },
            {field: 'fyzr_z', title: '配送类型', align: 'center', width: 25},
            {field: 'gylfyje', title: '配送费用(元)', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }
            },
            {field: 'gc_mc', title: '工程', align: 'center', width: 30},
            {field: 'xsgs_jc', title: '销售公司', align: 'center', width: 30},
            {field: 'sbgs_jc', title: '申报公司', align: 'center', width: 25},
            {field: 'sbbm_jc', title: '申报部门', align: 'center', width: 25},
            {field: 'sbr_xm', title: '申报人', align: 'center', width: 20},
            {field: 'cwsh', title: '审核状态', align: 'center', width: 20, formatter: function (value, rowData) {
                if (value == true)
                    return '已审核';
                else if (value == false)
                    return '未审核';
            }},
            {field: 'zt', title: '状态', align: 'center', width: 20, formatter: function (value, rowData) {
                if (value == 'ybc')
                    return '已保存';
                if (value == 'ytj')
                    return '已提交';
                if (value == 'ysp')
                    return '已审批';
            }}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}